#include <iostream>
#include <string>

using namespace std;


class Animal {
    protected:
        string name;

    public:
        Animal(const string &name) : name(name) {}
        virtual void makeSound() const {
            cout << name << " Animal sound" << endl;
        }
        virtual ~Animal() {} // 虚拟析构函数，防止子类对象被父类指针删除时内存泄漏
};


class Dog : public Animal {
    public:
        Dog(const string &name) : Animal(name) {}
        virtual void makeSound() const {
            cout << name << " Dog sound" << endl;
        }
};


class Cat : public Animal {
    public:
        Cat(const string &name) : Animal(name) {}
        virtual void makeSound() const {
            cout << name << " Cat sound" << endl;
        }
};


int main(void)
{
    // 创建一个 Animal 指针数组，存储对象
    Animal *animals[] = {
        new Animal("Lihua"),
        new Dog("Buddy"),
        new Cat("Whiskers"),
        new Dog("Max"),
        new Cat("Luna")
    };

    // 遍历数组，调用 makeSound()（多态行为）
    for (Animal *animal : animals)
        animal->makeSound();

    // 释放内存
    for (Animal *animal : animals)
        delete animal;

    return 0;
}

